package com.zy.openchat.core.model.sticker;

import android.os.Parcel;
import android.os.Parcelable;

import com.zy.openchat.Constants;
import com.zy.openchat.core.model.file.FileInfo;
import com.zy.openchat.core.model.photo.ThumbnailInfo;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

public class StickerInfo implements Parcelable {
    public long duration;
    public int width;
    public int height;
    private String file_name;
    private String mime_type;
    public ThumbnailInfo thumbnail;
    public FileInfo animation;


    public boolean isAnimationDownloaded() {
        if (animation != null && animation.local != null && animation.local.isExist()) {
            return true;
        }
        //本地路径，根据file_name判断
        if (!StringUtils.isEmpty(file_name)) {
            try {
                String path = Constants.userVideoPath() + file_name;
                File f = new File(path);
                if (f.exists()) {
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }


    public String localAnimationPath() {
        if (animation != null && animation.local != null && animation.local.isExist()) {
            return animation.local.path;
        }
        //本地路径，根据file_name获取
        if (!StringUtils.isEmpty(file_name)) {
            try {
                String path = Constants.userVideoPath() + file_name;
                File f = new File(path);
                if (f.exists()) {
                    return path;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public long getAnimationFileId(){
        if (animation != null) {
            return animation.id;
        }
        return 0;
    }

    public String getRemoteAnimationFileId(){
        if (animation != null && animation.remote!=null) {
            return animation.remote.id;
        }
        return "";
    }


    public void updateAnimationFile(FileInfo file) {
        if (animation != null ) {
            animation = file;
        }
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.duration);
        dest.writeInt(this.width);
        dest.writeInt(this.height);
        dest.writeString(this.file_name);
        dest.writeString(this.mime_type);
        dest.writeParcelable(this.thumbnail, flags);
        dest.writeParcelable(this.animation, flags);
    }

    public void readFromParcel(Parcel source) {
        this.duration = source.readLong();
        this.width = source.readInt();
        this.height = source.readInt();
        this.file_name = source.readString();
        this.mime_type = source.readString();
        this.thumbnail = source.readParcelable(ThumbnailInfo.class.getClassLoader());
        this.animation = source.readParcelable(FileInfo.class.getClassLoader());
    }

    public StickerInfo() {
    }

    protected StickerInfo(Parcel in) {
        this.duration = in.readLong();
        this.width = in.readInt();
        this.height = in.readInt();
        this.file_name = in.readString();
        this.mime_type = in.readString();
        this.thumbnail = in.readParcelable(ThumbnailInfo.class.getClassLoader());
        this.animation = in.readParcelable(FileInfo.class.getClassLoader());
    }

    public static final Creator<StickerInfo> CREATOR = new Creator<StickerInfo>() {
        @Override
        public StickerInfo createFromParcel(Parcel source) {
            return new StickerInfo(source);
        }

        @Override
        public StickerInfo[] newArray(int size) {
            return new StickerInfo[size];
        }
    };
}
